<template>
	<view class="content-img">
		<eui-nav-bar :title="cTitle" color="#000" rightIcon="" />
		<eui-search-bar v-model="searchValue" @confirm="selStoreById" radius="100" placeholder="请输入编号/姓名/拼音简写">
		</eui-search-bar>
		<view class="main-view">
			<view class="main-view-bg">
				<scroll-view scroll-y lower-threshold='88' @refresherrefresh="onRefresh" @scrolltolower="templateLower"
					:refresher-enabled='true' :refresher-triggered="triggered"
					:style="{width:scrollWidth+'px',height:scrollHeight}">
					<radio-group @change="setSelBackItem">
						<label class="padding-left-28 font-size-28 flex flex-direction-column background-fff"
							v-for="(item,index) in dataList" :key="index">
							<view class="sroll-store-list flex">
								<view class="flex align-items-center margin-right-10">
									<radio :value="item.c_userno" :checked="index === current" :color="themeColor"
										style="transform:scale(0.8)" />
								</view>
								<view class="flex align-items-center font-weight-bold">
									[{{item.c_userno}}]{{item.c_name}}</view>
							</view>
						</label>
					</radio-group>
				</scroll-view>
			</view>
		</view>
		<eui-popup ref="popup4" type="center" :mask-click="false">
			<view class="batchApproveResult" :style="{width:(scrollWidth-20)+'px'}">
				<view style="padding: 15px 10px;">
					请输入审批意见
					<input type="text" v-model="cNote" placeholder="请输入审批意见，建议100字以内..."
						class="popup-textarea margin-top-20" />
					<!-- <textarea v-model="cNote" auto-height placeholder="请输入审批意见，建议100字以内..."
						class="popup-textarea margin-top-20">
					</textarea> -->
				</view>
				<view class="padding-30 flex">
					<eui-button type="min2" title="取消" @click="CancelApprove" />
					<eui-button type="min" title="确认" @click="ConfirmApprove" />
				</view>
			</view>
		</eui-popup>
	</view>
</template>

<script setup type="ts">
	import { onLoad } from '@dcloudio/uni-app';
	import { ref } from 'vue';
	import service from '@/common/service';
	import { storeToRefs } from 'pinia';
	import { useMainStore } from '@/store/modules/store';
	import { useLoginStore } from '@/store/modules/useLoginStore';
	import { useThemeStore } from '@/store/themeStore';
	const mainStore = useMainStore();
	const LoginStore = useLoginStore();
	const themeStore = useThemeStore(); // 主题
	const { theme } = storeToRefs(themeStore);
	const themeColor = ref(themeStore.theme == 'theme-blue' ? '#02B7E6' : '#FF6430')
	
	const statusBarHeight = ref(uni.getSystemInfoSync().statusBarHeight + 'px');
	const scrollWidth = ref(uni.getSystemInfoSync().windowWidth - 20);
	const scrollHeight = ref((uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().statusBarHeight - 105) + 'px');
	const dataList = ref([]);
	const searchValue = ref('');
	const triggered = ref(false);
	const PageSize = ref(30);
	const pageList = ref({
		CurrentPage: 1, //第几页
		RowsCount: 0, //总条数
		PageCount: 1, //总页数
	});
	const reqUser = ref({});//前插后插人员信息
	const popup4 = ref(null);
	const cNote = ref('');
	const cTitle = ref('选择人员');
	const approveInfo = ref({});
	const cType = ref('');
	const current = ref(-1);
	
	const selStoreById =()=>{
		current.value = -1;
		onRefresh();
	}
	// 下拉刷新
	const onRefresh = ()=>{
		if (triggered.value) {
			return false;
		};
		triggered.value = true;
		
		pageList.value.CurrentPage = 1;
		dataList.value = [];
		GetUserListForDialog();
		
		setTimeout(() => {
			triggered.value = false;
		}, 1000)
	}
	// 加载更多
	const templateLower = ()=>{
		if (pageList.value.CurrentPage < pageList.value.PageCount) {
			pageList.value.CurrentPage = pageList.value.CurrentPage + 1;
			GetUserListForDialog();
		}
	}
	const setSelBackItem = e=>{
		console.log("e.detail: " + JSON.stringify(e.detail));
		for (let i = 0; i < dataList.value.length; i++) {
			if (dataList.value[i].c_userno === e.detail.value) {
				current.value = i;
				const info = dataList.value[i];
				if (info.c_userno == mainStore.loginInfo.sUser) {
					uni.showToast({
						duration:3500,
						icon: 'none',
						title: '对不起，您只能选择自己以外的人员！'
					});
					return false;
				}
				reqUser.value = {
					UserNo: info.c_userno,
					UserName: '[' + info.c_userno + ']' + info.c_name,
				}
				cNote.value = '';
				if(cType.value=='后插审批人'){
					popup4.value.open();
				}else{
					uni.showModal({
						title: '提示',
						content: '确认插入所选的人进行审批吗?',
						success: function(res) {
							if (res.confirm) {
								ApproveNodeExcute(5, '');
							} else if (res.cancel) {
								current.value = -1;
								console.log('用户点击取消');
							}
						}
					});
				}
				break;
			}
		}
	}
	// 同意或者驳回
	const ConfirmApprove = () => {
		ApproveNodeExcute(6,cNote.value);
		CancelApprove();
	}
	const CancelApprove = () => {
		current.value = -1;
		popup4.value.close();
	}
	const GetUserListForDialog = () =>{
		service.enjoyShowLoading('正在加载...');
		let WhereList = [];
		if(searchValue.value){
			WhereList = [{
					c_left: '(',
					c_type_name: '',
					c_col: 'p.c_name',
					c_oper: '相似',
					c_value: '%' + searchValue.value + '%',
					c_col_dtype: "System.String",
					c_logic: "或者",
					c_right: ")",
				},
				{
					c_left: '(',
					c_type_name: '',
					c_col: 'c_userno',
					c_oper: '相似',
					c_value: '%' + searchValue.value + '%',
					c_col_dtype: "System.String",
					c_logic: "或者",
					c_right: ")",
				},
			];
		}
		let request = {
		    url: LoginStore.EnjoyMain,
		    data: {
		        UniqueKey: "管易通40通讯录",
		        ObjectData: {
					IsNotPage: false,
					PageSize: PageSize.value,
					Sql: null,
					WhereList: WhereList,
					...pageList.value
				},//mainStore.loginInfo.sUser,
		        MethodName: "GetUserListForDialog",
		    },
		}
		service.request(request, (res) => {
		    if (res && res.data) {
		        const ObjectData = res.data.ObjectData;
		        if (!res.data.HasException) {
					const ObjectData = res.data.ObjectData;
					pageList.value.RowsCount = ObjectData.RowsCount;
					pageList.value.PageCount = ObjectData.PageCount;
					pageList.value.CurrentPage = ObjectData.CurrentPage;
					
					const list = ObjectData.ReturnObject;
					if (list.length > 0) {
						list.map((item, index) => {
							if (item.c_name != 'Group') {
								dataList.value.push({
									...item,
								});
							}
						})
					}
		        } else {
		            service.enjoyShowToast(res.data.Exception.Message)
		        }
		    }
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
		    console.log('失败')
		});
	}
	const ApproveNodeExcute = (AppOperType, note) => {
		service.enjoyShowLoading('正在处理...');
		let request = {
			url: LoginStore.EnjoyMain,
			data: {
				UniqueKey: "管易通40审批",
				ObjectData: {
					AppOperType: AppOperType,
					ReqUser:reqUser.value,
					SetpName: '',
					AppUser: '',
					AppSendUserNo: '',
					AppDeFineGuid: '',
					AppDeFineType: '',
					AppDetail: note,
					AppUserNo: mainStore.loginInfo.sUser,
					AppUserName: mainStore.loginInfo.sUserName,
					BillName: approveInfo.value.BillName,
					BillRealName: approveInfo.value.BillRealType,
					NodeId: approveInfo.value.AppNodeId,
					BillId: approveInfo.value.BillKey,
					...approveInfo.value,
				},
				MethodName: "ApproveExcute",
			},
		}
		service.request(request, (res) => {
			if (res && res.data) {
				if (!res.data.HasException) {
					const ObjectData = res.data.ObjectData;
					if (ObjectData != undefined && ObjectData !=null && ObjectData.AlterInFo != undefined &&ObjectData.AlterInFo != null && ObjectData.AlterInFo != '') {
						uni.showToast({
							icon: 'none',
							title: '审批失败:' + ObjectData
								.AlterInFo,
							mask: true,
							duration: 3500,
						});
					} else {
						SetCurrentPages();
						uni.navigateBack({
							delta: 1
						});
					}
				} else {
					service.enjoyShowToast(res.data.Exception.Message)
				}
			}
			service.enjoyHideLoading();
		}, () => {
			service.enjoyHideLoading();
			console.log('失败')
		});
	}
	const SetCurrentPages = () =>{
		try {
			let pages = getCurrentPages();
			let prevPage = pages[pages.length - 2];
			prevPage.$vm.$.exposed.onRefresh();
		} catch (e) {
		}
	}
	onLoad(e=>{
		if(e.type){
			cTitle.value = '选择'+e.type;
		}
		if(e.item){
			approveInfo.value = JSON.parse(e.item);
			console.log("approveInfo.value: " + JSON.stringify(approveInfo.value));
			cType.value = e.type;
		}
		onRefresh();
	})
</script>

<style lang="scss" scoped>
	@import '@/main.scss';

	.content-img {
		background-color: #FFFFFF;
		background-repeat: no-repeat;
		width: 100%;
	}

	.header-content {
		height: 88rpx;
		padding: 0 6px;
		background-color: #fff;
		align-items: center;
	}

	.flex {
		display: flex;
	}

	.flex-1 {
		flex: 1;
	}

	.justify-content-space-between {
		justify-content: space-between !important;
	}

	.main-view {
		flex: 1;
		flex-direction: column;
		padding: 20rpx;
		background-color: #f2f2f2;
	}

	.main-view-bg {
		// background-color: #fff;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.background-fff {
		background-color: #fff;
	}

	.background-fff:first-child {
		border-top-left-radius: 20rpx;
		border-top-right-radius: 20rpx;
	}

	.background-fff:last-child {
		border-bottom-left-radius: 20rpx;
		border-bottom-right-radius: 20rpx;
	}

	.background-fff:last-child .sroll-store-list {
		border-bottom: none;
	}

	.padding-left-28 {
		padding-left: 28rpx;
	}

	.font-size-28 {
		font-size: 28rpx;
	}

	.flex-direction-column {
		flex-direction: column !important;
	}

	.store-checked {
		background-color: rgba(249, 250, 252, 1) !important;
	}

	.sroll-store-list {
		padding: 20rpx 28rpx 20rpx 0;
		border-bottom: solid 1px rgba(238, 238, 238, 1);
		line-height: 1;
	}

	.color-888 {
		color: rgba(136, 136, 136, 1);
	}

	.margin-top-20 {
		margin-top: 20rpx;
	}

	.batchApproveResult {
		background-color: rgb(255, 255, 255);
		border-radius: 6px;
		font-size: 14px;
	}

	.padding-30 {
		padding: 10rpx 30rpx;
	}

	.popup-textarea {
		font-size: 14px;
		background-color: rgb(249, 249, 248);
		margin-top: 10px;
		width: 100%;
		padding: 5px;
		box-sizing: border-box;
		border-radius: 4px;
		height: 35px;
	}

	.align-items-center {
		align-items: center;
	}

	.margin-right-10 {
		margin-right: 10rpx;
	}

	.font-weight-bold {
		font-weight: 600;
	}
</style>